package com.serviceprovider.business.module.order.controller;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.admin.address.dao.impl.GeoDao;
import com.serviceprovider.business.module.admin.address.model.State;
import com.serviceprovider.business.module.admin.role.services.dao.interfaces.IRoleService;
import com.serviceprovider.business.module.admin.user.services.dao.interfaces.IUserService;
import com.serviceprovider.business.module.common.Page;
import com.serviceprovider.business.module.order.model.ConditionType;
import com.serviceprovider.business.module.order.model.EnumTitlePair;
import com.serviceprovider.business.module.order.model.OrderFilterConditionForm;
import com.serviceprovider.business.module.order.model.OrderStatus;
import com.serviceprovider.business.module.order.services.interfaces.IOrderFiltersService;
import com.serviceprovider.business.module.order.services.interfaces.IOrderStatusService;
import com.serviceprovider.business.module.order.valueobjects.OrderAttributeVO;
import com.serviceprovider.business.module.order.valueobjects.OrderFilterVO;
import com.serviceprovider.business.module.user.model.impl.User;
import com.serviceprovider.business.utility.ApplicationConfigProperties;
import com.serviceprovider.business.utility.RequestConstants;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

//~--- JDK imports ------------------------------------------------------------

import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/admin/order/view_orders_filters.htm")
public class ViewOrderFiltersController {
    @Autowired
    private IOrderFiltersService        orderFiltersService;
    @Autowired
    private IOrderStatusService         orderStatusService;
    @Autowired
    private ApplicationConfigProperties properties;
    @Autowired
    private IUserService                providerService;
    @Autowired
    private IRoleService                roleService;
    @Autowired
    private GeoDao                      stateDao;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(properties.getDateFormat());

        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView viewOrderFilters() {
        return refreshList();
    }

    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView addNewOrderFilter() {
        ModelAndView             result        = new ModelAndView(RequestConstants.OrderRequests.VIEW_ORDERS_FILTERS_NEW_VIEW);
        OrderFilterConditionForm newFilterForm = new OrderFilterConditionForm();

        result.addObject("newFilterForm", newFilterForm);

        List<EnumTitlePair> conditionTypeList = ConditionType.getTitles();

        result.addObject("conditionTypeList", conditionTypeList);

        return result;
    }

    @RequestMapping(
        method = RequestMethod.POST,
        params = "add"
    )
    public ModelAndView saveNewOrderFilter(@ModelAttribute OrderFilterConditionForm form) {
        if (form.getId() == null) {
            orderFiltersService.addNew(form.convertToVO());
        } else {
            orderFiltersService.update(form.convertToVO());
        }

        return refreshList();
    }

    private ModelAndView refreshList() {
        ModelAndView        result     = new ModelAndView(RequestConstants.OrderRequests.VIEW_ORDERS_FILTERS_VIEW);
        List<OrderFilterVO> filterList = orderFiltersService.findAll();

        result.addObject("filterList", filterList);

        return result;
    }

    @RequestMapping(
        method = RequestMethod.GET,
        params = { "remove" }
    )
    public ModelAndView removeOrderFilter(@RequestParam("id") Integer id) {
        orderFiltersService.removeById(id);

        return refreshList();
    }

    @RequestMapping(
        method = RequestMethod.GET,
        params = { "edit" }
    )
    public ModelAndView editOrderFilter(@RequestParam("id") Integer orderFilterId) {
        ModelAndView             result        = new ModelAndView(RequestConstants.OrderRequests.VIEW_ORDERS_FILTERS_NEW_VIEW);
        OrderFilterVO            orderFilter   = orderFiltersService.getById(orderFilterId);
        OrderFilterConditionForm newFilterForm = new OrderFilterConditionForm(orderFilter);

        if (newFilterForm.getStatusId() == null) {    // Displaying "Any" status
            newFilterForm.setStatusId(0);
        }

        result.addObject("newFilterForm", newFilterForm);

        List<EnumTitlePair> conditionTypeList = ConditionType.getTitles();

        result.addObject("conditionTypeList", conditionTypeList);

        return result;
    }

    @RequestMapping(
        method = RequestMethod.POST,
        params = "cancel_add"
    )
    public ModelAndView cancelAddNewOrderFilter(@ModelAttribute OrderAttributeVO orderAttribute) {
        return refreshList();
    }

    @ModelAttribute("statusList")
    public List<OrderStatus> getStatusList() {
        return orderStatusService.findAll();
    }

    @ModelAttribute("stateList")
    public List<State> getStateList() {
        return stateDao.getAllStates();
    }

    @ModelAttribute("providerList")
    public Page<User> getProviderList() {
        return providerService.findByRole(roleService.getRoleByName("ROLE_SERVICE_PROVIDER"));
    }
}



